package com.cst.zju.anime.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cst.zju.anime.dao.AnimeAndTagsMapper;
import com.cst.zju.anime.dao.AnimeMapper;
import com.cst.zju.anime.dao.DetailMapper;
import com.cst.zju.anime.dao.TagsMapper;
import com.cst.zju.anime.model.Anime;
import com.cst.zju.anime.model.AnimeAndTagsExample;
import com.cst.zju.anime.model.AnimeAndTagsKey;
import com.cst.zju.anime.model.AnimeExample;
import com.cst.zju.anime.model.AnimeExample.Criteria;
import com.cst.zju.anime.model.DetailExample;
import com.cst.zju.anime.model.Tags;
import com.cst.zju.anime.model.TagsExample;
import com.cst.zju.anime.service.IAnimeService;
import com.cst.zju.anime.service.utils.DateUtils;
import com.cst.zju.anime.utils.Page;

@Transactional
@Service(value="iAnimeService")
public class AnimeService implements IAnimeService {

	@Resource
	private DetailMapper detailMapper;
	@Resource
	private AnimeMapper animeMapper;
	@Resource
	private TagsMapper tagsMapper;
	@Resource
	private AnimeAndTagsMapper animeAndTagsMapper;


	@Override
	public List<Anime> getNewSerialAnime(int pageNum,int pageSize){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andStatusLike("%连载%");
		example.setOrderByClause("updatedate DESC");
		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}


	@Override
	public List<Anime> getNewCompletionAnime(int pageNum, int pageSize){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andStatusLike("%集全%");
		example.setOrderByClause("updatedate DESC");
		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}

	@Override
	public List<Anime> getRecommendSerialAnime(int pageNum,int pageSize){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andStatusLike("%连载%");
		example.setOrderByClause("downcount DESC");
		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}

	@Override
	public List<Anime> getRecommendCompletionAnime(int pageNum,int pageSize){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andStatusLike("%集全%");
		example.setOrderByClause("downcount DESC");
		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}


	@Override
	public List<Anime> getHotSeasonAnime(int pageNum, int pageSize, String season){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		DetailExample example = new DetailExample();
		example.or().andSeasonEqualTo(season);

		List<Integer> list = new ArrayList<Integer>();
		list = this.detailMapper.selectIdByExample(example);

		AnimeExample example2 = new AnimeExample();
		example2.or().andIdIn(list);
		example2.setOrderByClause("updatedate DESC");
		example2.setPage(page);
		
		return this.animeMapper.selectByExample(example2);
	}

	@Override
	public List<Anime> getAnimeRankAllList(int pageNum,int pageSize){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);

		AnimeExample example = new AnimeExample();
		example.setOrderByClause("downcount DESC");
		page.setTotalRecords(this.animeMapper.countByExample(example));
		example.setPage(page);

		return this.animeMapper.selectByExample(example);
		
	}

	@Override
	public List<Anime> getAnimeRankListBySeason(int pageNum, int pageSize, String season){
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		DetailExample example = new DetailExample();
		example.or().andSeasonEqualTo(season);

		List<Integer> list = new ArrayList<Integer>();
		list = this.detailMapper.selectIdByExample(example);

		AnimeExample example2 = new AnimeExample();
		example2.or().andIdIn(list);
		example2.setOrderByClause("updatedate, downcount DESC");
		example2.setPage(page);

		return this.animeMapper.selectByExample(example2);
	}

	@Override
	public Page<Anime> getAnimeBySearch(int pageNum, int pageSize, String name) {

		Page<Anime> page = new Page<Anime>(pageNum, pageSize, 7);
		AnimeExample example = new AnimeExample();
		example.or().andNameLike("%"+name+"%");
		example.setOrderByClause("downcount DESC");
		page.setTotalRecords(this.animeMapper.countByExample(example));
		example.setPage(page);
		page.setResultList(animeMapper.selectByExample(example));
		return page;
	}

	@Override
	public Page<Anime> getNewAnimeByFilter(String type, int pageNum, int pageSize, Integer classifyId, String tag) {
		List<Anime> list = null;
		
		Page<Anime> page = new Page<Anime>(pageNum,pageSize,7);
		
		//根据标签查找中间表对象
		TagsExample tagsExample = new TagsExample();
		tagsExample.createCriteria().andNameLike("%"+tag+"%");
		List<Tags> tagsList = tagsMapper.selectByExample(tagsExample);
		List<Integer> idList = new ArrayList<Integer>();
		
		//根据中间表对象查找动漫Id
		tagsList.forEach((Tags tags) -> idList.add(tags.getId()));
		AnimeAndTagsExample animeAndTagsExample = new AnimeAndTagsExample();
		animeAndTagsExample.createCriteria().andTagsIdIn(idList);
		List<AnimeAndTagsKey> animeAndTagsList = animeAndTagsMapper.selectByExample(animeAndTagsExample);
		idList.clear();
		animeAndTagsList.forEach((AnimeAndTagsKey animeAndTags) -> idList.add(animeAndTags.getAnimeId()));
		
		//根据动漫Id和分类id查找动漫
		AnimeExample animeExample = new AnimeExample();
		Criteria criteria = animeExample.createCriteria();
		criteria.andIdIn(idList);
		if(classifyId != null){
			criteria.andClassifyIdEqualTo(classifyId);
		}
		criteria.andStatusLike("%"+ type +"%");
		animeExample.setOrderByClause("updatedate DESC");
		page.setTotalRecords(this.animeMapper.countByExample(animeExample));
		animeExample.setPage(page);
		list = animeMapper.selectByExample(animeExample);
		
		if(list.isEmpty()){
			return page;
		}
		page.setResultList(list);
		return page;
	}

	@Override
	public Page<Anime> getHotAnimeByFilter(String type, int pageNum, int pageSize, Integer classifyId, String tag) {
		List<Anime> list = null;
		Page<Anime> page = new Page<Anime>(pageNum,pageSize,7);
		
		//根据标签查找中间表对象
		TagsExample tagsExample = new TagsExample();
		tagsExample.createCriteria().andNameLike("%"+tag+"%");
		List<Tags> tagsList = tagsMapper.selectByExample(tagsExample);
		List<Integer> idList = new ArrayList<Integer>();
		
		//根据中间表对象查找动漫Id
		tagsList.forEach((Tags tags) -> idList.add(tags.getId()));
		AnimeAndTagsExample animeAndTagsExample = new AnimeAndTagsExample();
		animeAndTagsExample.createCriteria().andTagsIdIn(idList);
		List<AnimeAndTagsKey> animeAndTagsList = animeAndTagsMapper.selectByExample(animeAndTagsExample);
		idList.clear();
		animeAndTagsList.forEach((AnimeAndTagsKey animeAndTags) -> idList.add(animeAndTags.getAnimeId()));
		
		//根据动漫Id和分类id查找动漫
		AnimeExample animeExample = new AnimeExample();
		Criteria criteria = animeExample.createCriteria();
		criteria.andIdIn(idList);
		if(classifyId != null){
			criteria.andClassifyIdEqualTo(classifyId);
		}
		criteria.andStatusLike("%"+ type +"%");
		animeExample.setOrderByClause("downcount DESC");
		
		page.setTotalRecords(this.animeMapper.countByExample(animeExample));
		animeExample.setPage(page);
		list = animeMapper.selectByExample(animeExample);
		
		if(list.isEmpty()){
			return page;
		}
		page.setResultList(list);
		return page;
	}

	@Override
	public List<Anime> getAnimeRankOfWeekList(int pageNum, int pageSize) {
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andUpdatedateBetween(DateUtils.getTimesWeekmorning(), DateUtils.getTimesWeeknight());
		example.setOrderByClause("downcount DESC");

		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}

	@Override
	public List<Anime> getAnimeRankOfMonthList(int pageNum, int pageSize) {
		Page<Anime> page = new Page<Anime>(pageNum,pageSize);
		AnimeExample example = new AnimeExample();
		example.createCriteria().andUpdatedateBetween(DateUtils.getTimesMonthmorning(), DateUtils.getTimesMonthnight());
		example.setOrderByClause("downcount DESC");

		example.setPage(page);

		return this.animeMapper.selectByExample(example);
	}

}
